<template>
  <div>
    <h2>随机点名</h2>
    <div class="box">
      <span>名字是：</span>
      <div class="qs">{{ word }}</div>
    </div>
    <div class="btns">
      <button @click="btnStart" class="start" :disabled="iscode">开始</button>
      <button @click="btnEnd" class="end" :disabled="iscode">结束</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: ["马超", "赵云", "黄忠", "貂蝉", "张飞"],
      //显示抽取到的人数
      word: "这里显示姓名",
      //定时函数
      timer: "",
      //是否禁用按钮
      iscode:false,
      //jianname表示获取或者是抽到过的同学
      jianname: "",
    };
  },


  methods: {
    //开始按钮的事件
    btnStart() {
      if(this.timer)clearInterval(this.timer);
      //创建一个定时器
      this.timer = setInterval(() => {
        //通过数组长度（0，arr.length）获取随机数
        let num = Math.floor(Math.random() * this.arr.length);
        //抽取到的姓名
        this.word = this.arr[num];
        //记录抽到过的同学
        this.jianname = this.arr[num];
      }, 100);
    },
    //结束按钮
    btnEnd() {
      clearInterval(this.timer);
      // 创建一个定时器
      this.arr.forEach((item, index) => {
        //如果遍历的数组为抽取到的人员，则将其从数组中删除
        if (item == this.jianname) {
          this.arr.splice(index, 1);
        }
    // this.arr.splice(this.num, 1);
        if (this.arr.length ==0) {
          this.iscode = true;
          this.word = '没有数据了';

        
        }
      });
    },
  },
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
}

h2 {
  text-align: center;
}

.box {
  width: 600px;
  margin: 50px auto;
  display: flex;
  font-size: 25px;
  line-height: 40px;
}

.qs {
  width: 450px;
  height: 40px;
  color: red;
}

.btns {
  text-align: center;
}

.btns button {
  width: 120px;
  height: 35px;
  margin: 0 50px;
}
</style>